home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Programming Stuff / Sample Code / Printing Samples / Printer Drivers… / ImageWriter--custom dialogs / ChooserSupport.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  7.7 KB  |  290 lines  |  [TEXT/MPS ]

  1. /*
  2.     ChooserSupport.c - C code for PACK and LDEF resources used by the Chooser.
  3.     
  4.     Copyright © 1992-1996 Apple Computer, Inc.
  5.     All rights reserved.
  6.  
  7.     12/20/93        dmh                Sync'd with the shipping 1.0b3 GX driver.
  8.     12/22/93        dmh                Added custom dialog code.
  9.      6/14/96        cn                Updated to support Universal Interfaces 2.1.
  10. */
  11.  
  12. #include <Types.h>
  13. #include <QuickDraw.h>
  14. #include <Fonts.h>
  15. #include <Lists.h>
  16. #include <Devices.h>
  17. #include <Resources.h>
  18. #include <Script.h>
  19. #include <ToolUtils.h>
  20. #include <LowMem.h>
  21.  
  22. #include <GXGraphics.h>
  23. #include <GXPrinterDrivers.h>
  24.  
  25. // ------------------------------------------------------------------------
  26. // INTERNAL DEFINES
  27. // ------------------------------------------------------------------------
  28. // Chooser initialize message selector
  29. #define initializeMsg    11
  30.  
  31. // Icon Suite support
  32. #define ttNone        0x0000
  33. #define ttDisabled    0x0001
  34. #define    ttOffline    0x0002
  35. #define ttOpen        0x0003
  36. #define ttSelected     0x4000
  37. #define ttSelectedDisabled    (ttSelected + ttDisabled)
  38. #define ttSelectedOffline    (ttSelected + ttOffline)
  39. #define ttSelectedOpen        (ttSelected + ttOpen)
  40.  
  41. #define ttLabel0    0x0000
  42. #define ttLabel1    0x0100
  43. #define ttLabel2    0x0200
  44. #define ttLabel3    0x0300
  45. #define ttLabel4    0x0400
  46. #define ttLabel5    0x0500
  47. #define ttLabel6    0x0600
  48. #define ttLabel7    0x0700
  49. pascal OSErr PlotIconSuite(const Rect * theRect, short align, short iconTransform, Handle cIcon)
  50.     = {0x303C, 0x0603, 0xABC9};
  51.  
  52. // Copy of the DrawText trap
  53. pascal void OldDrawText(const void *textBuf,short firstByte,short byteCount)
  54.     = 0xA885; 
  55.  
  56. // ------------------------------------------------------------------------
  57. // MAIN CODE FOR PACK
  58. // ------------------------------------------------------------------------
  59. pascal OSErr Device(short message, short caller, StringPtr objName, 
  60.     StringPtr zoneName, ListHandle theList, long p2)
  61. {
  62.     
  63.     OSErr            anErr = noErr;
  64.     extern Str31     gDriverName;
  65.     StringPtr        pDriverName = gDriverName;
  66.     extern gxJob    gJob;
  67.     gxJob            *pJob = &gJob;
  68.     
  69.     // start up GX to begin with
  70.     if (message == initializeMsg)
  71.         {
  72.         FCBPBRec    pb;
  73.  
  74.         // determine the driver name
  75.         pb.ioCompletion     = nil;
  76.         pb.ioNamePtr         = pDriverName;
  77.         pb.ioVRefNum         = 0;
  78.         pb.ioRefNum         = CurResFile();
  79.         pb.ioFCBIndx         = 0;
  80.         anErr = PBGetFCBInfo(&pb, false);
  81.  
  82.         *pJob = nil;
  83.         if (anErr == noErr)
  84.             {
  85.             GXEnterGraphics();
  86.             anErr = GXGetGraphicsError(nil);
  87.             if (anErr == noErr)
  88.                 {
  89.                 anErr = GXInitPrinting();
  90.                 if (anErr != noErr)
  91.                     GXExitGraphics();
  92.                 }
  93.                 
  94.             if (anErr != noErr)
  95.                 StopAlert(-4095, nil);
  96.             }
  97.         }
  98.         
  99.     // let the system handle the choosing for us (how nice of it!)
  100.     if (anErr == noErr)
  101.         {
  102.         if ((*pJob != nil) || (message == initializeMsg))
  103.             {
  104.             anErr = GXHandleChooserMessage(pJob, pDriverName, message, caller, objName, zoneName, theList, p2);
  105.         
  106.             // tear down GX when done
  107.             if ( (message == terminateMsg) && (p2 == terminateMsg) )
  108.                 {
  109.                 GXExitPrinting();
  110.                 GXExitGraphics();
  111.                 }
  112.             }
  113.         }
  114.         
  115.     return(anErr);
  116.     
  117. } // Device
  118.  
  119.  
  120.  
  121. // ------------------------------------------------------------------------
  122. // ENTRY POINT FOR LDEF
  123. // ------------------------------------------------------------------------
  124.  
  125. pascal void LDEF(
  126.     short         message,        // What operation to perform on list
  127.     Boolean     select,            // Is this cell to be selected or not?
  128.     Rect        *theRect,        // Rectangle of this cell, clipped to window
  129.     Cell        theCell,        // Which cell this is
  130.     short        dataOffset,        // Offset into data for this cell
  131.     short        dataLen,        // Length of data for this cell
  132.     ListHandle    theList)        // The list to act upon
  133. /*
  134.     An LDEF that works in two modes:
  135.         - if the first two characters of the cell are valid AppleTalk NBP names (ie, not ≈ ≈)
  136.           then the LDEF is just a basic text LDEF
  137.         - otherwise, it assumes the data is part of a PortListRec, which is
  138.           a structure for icons with text underneath
  139. */
  140.  
  141. {
  142. #pragma unused (theCell, dataLen)
  143.  
  144.     gxPortListRec        theCellContents;
  145.     Rect                iconRect;
  146.     
  147.     switch (message)
  148.         {
  149.         case lDrawMsg:
  150.         case lHiliteMsg:
  151.         
  152.             // save the data to avoid locking things down
  153.             if (dataLen > sizeof(theCellContents) )
  154.                 dataLen = sizeof(theCellContents);
  155.             BlockMove(((*(**theList).cells) + dataOffset), &theCellContents, dataLen );
  156.             
  157.             // draw the cell as an icon, but only if we see our magic marker at the front
  158.             if ( (theCellContents.firstMarker == '≈') && (theCellContents.secondMarker == '≈') )
  159.                 {
  160.                 // center the icon rect on the list with a top margin of 10 pixels
  161.                 iconRect.top = theRect->top + 10;
  162.                 iconRect.left = theRect->left + ((theRect->right - theRect->left) >> 1) - 16;
  163.                 iconRect.bottom = iconRect.top + 32;
  164.                 iconRect.right = iconRect.left + 32;
  165.                 
  166.                 
  167.                 // draw the icon
  168.                 if (theCellContents.iconSuiteHandle != nil)
  169.                     PlotIconSuite(&iconRect,
  170.                             ttNone, (select) ? ttSelected: ttNone,
  171.                             theCellContents.iconSuiteHandle);
  172.                             
  173.                 // Get the general area under the icon in which to draw the label
  174.                 iconRect.left = theRect->left + 2;
  175.                 iconRect.right = iconRect.left + (**theList).cellSize.h - 2;
  176.                 iconRect.top = iconRect.bottom + 2;
  177.                 iconRect.bottom = theRect->bottom;
  178.     
  179.                 // use a nice small font for the label            
  180.                 TextFont(applFont);
  181.                 TextSize(9);
  182.                 
  183.                     {
  184.                     short        labelWidth;
  185.                     short        rectWidth;
  186.                     short        labelHeight;
  187.                     FontInfo    theInfo;
  188.                 
  189.                     // Get rid of any text that was there before
  190.                     EraseRect(&iconRect);
  191.                     iconRect.top += 2;
  192.                     
  193.                     // compute the height of the label                    
  194.                     GetFontInfo(&theInfo);
  195.                     labelHeight = theInfo.ascent + theInfo.leading;
  196.                     
  197.                     // compute where to draw the text
  198.                     iconRect.bottom = iconRect.top + labelHeight;
  199.                     rectWidth = iconRect.right-iconRect.left;
  200.                     
  201.                     // truncate the string to fit within the box
  202.                     TruncString(rectWidth, theCellContents.iconName, smTruncEnd);
  203.                     
  204.                     // compute the new width of the string
  205.                     labelWidth = StringWidth(theCellContents.iconName);
  206.                     
  207.                     // center the string, draw it
  208.                     iconRect.left += (rectWidth >> 1) - (labelWidth >> 1);
  209.                     MoveTo(iconRect.left, iconRect.bottom);
  210.                     DrawString(theCellContents.iconName);
  211.                     
  212.                     if (select)
  213.                         {
  214.                         // compute right and lower edge of box bounding the text we just drew
  215.                         iconRect.right = iconRect.left + labelWidth;
  216.                         iconRect.bottom += theInfo.descent;
  217.                         
  218.                         // outset it, and invert it to select it
  219.                         InsetRect(&iconRect, -1, -1);
  220.                             {
  221.                             UInt8    mode;
  222.                             
  223.                             mode = LMGetHiliteMode ();
  224.                             BitClr(&mode, pHiliteBit);
  225.                             LMSetHiliteMode (mode);
  226.                             }
  227.                         InvertRect(&iconRect);
  228.                         }
  229.                     }
  230.                     
  231.                 TextFont(applFont);
  232.                 TextSize(0);
  233.                 }
  234.             else
  235.                 {
  236.                 // how boring!  It's only text
  237.                 FontInfo    theInfo;
  238.                 Rect        ourRect;
  239.                 short        cellWidth;
  240.                 
  241.                 // add a margin to the rectangle
  242.                 ourRect = *theRect;
  243.                 ourRect.left += 4;
  244.                 --ourRect.right;
  245.                 cellWidth = ourRect.right - ourRect.left;
  246.                 
  247.                 // erase the rectangle
  248.                 GetFontInfo(&theInfo);
  249.                 EraseRect(theRect);
  250.                 MoveTo(ourRect.left, ourRect.bottom - theInfo.descent);
  251.                 
  252.                 // hey, you can't park that string here -- it's too big!
  253.                 if (TextWidth((Ptr) &theCellContents, 0, dataLen) > cellWidth )
  254.                     {
  255.                     // condense the text first
  256.                     TextFace(condense);
  257.                     
  258.                     // then truncate afterwards
  259.                     TruncText(cellWidth, (Ptr) &theCellContents, &dataLen, smTruncEnd);
  260.                     }
  261.                     
  262.                 // those darn other languages!
  263.                 if (GetSysJust() == teJustRight)
  264.                     Move(cellWidth-TextWidth((Ptr) &theCellContents, 0, dataLen) , 0);
  265.                     
  266.                 OldDrawText((Ptr) &theCellContents, 0, dataLen);
  267.                 
  268.                 // if selected, invert it
  269.                 if (select)
  270.                     {
  271.                         {
  272.                         UInt8    mode;
  273.                         
  274.                         mode = LMGetHiliteMode ();
  275.                         BitClr(&mode, pHiliteBit);
  276.                         LMSetHiliteMode (mode);
  277.                         }
  278.                     InvertRect(theRect);
  279.                     }
  280.                     
  281.                 // normal text again
  282.                 TextFace(normal);
  283.                 }
  284.                 
  285.             break;
  286.             
  287.         } // switch
  288.         
  289. } // LDEF
  290.